using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems; // 关键命名空间

public class 物品信息显示 : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    private GameObject tooltipPanel;
    [SerializeField] 
    private GameObject targetGameObject;
    // 获取RectTransform组件
    private RectTransform tooltipRect;
    private RectTransform targetRect;

    private void Start()
    {
        tooltipPanel = 背包信息显示.BPiteminfo.物品介绍;
        // 获取RectTransform组件
        tooltipRect = tooltipPanel.GetComponent<RectTransform>();
        targetRect = targetGameObject.GetComponent<RectTransform>();
    }

    // 当鼠标指针进入该UI区域时自动调用
    public void OnPointerEnter(PointerEventData eventData)
    {
        // 获取 tooltipRect 的长和宽
        float tooltipWidth = tooltipRect.rect.width;
        float tooltipHeight = tooltipRect.rect.height;
        // 获取 targetRect 的长和宽
        float targetWidth = targetRect.rect.width;
        float targetHeight = targetRect.rect.height;
        Vector3 newposition=gameObject.transform.position;

        newposition.x += (tooltipWidth+ targetWidth)/2;
        // 获取当前游戏窗口的宽高
        int gameWindowWidth = Screen.width;
        int gameWindowHeight = Screen.height;
       
        //限制信息栏的位置在屏幕中
        newposition.y = Math.Min(gameWindowHeight-50 - tooltipHeight / 2, newposition.y);
        newposition.y = Math.Max(tooltipHeight / 2, newposition.y);

        tooltipPanel.transform.position = newposition;
        tooltipPanel.SetActive(true);

        //获取物品的介绍
        TMP_Text targetText = tooltipPanel.GetComponentInChildren<TMP_Text>();
        targetText.text = targetRect.GetComponent<物品单位>().iteminfo;
        
    }

    // 当鼠标指针离开该UI区域时自动调用
    public void OnPointerExit(PointerEventData eventData)
    {
        tooltipPanel.SetActive(false);
    }


}